//
//  ViewController.m
//  wnagyi_news
//
//  Created by Guo Rui on 2017/9/13.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "TopTableBarViewController.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

NSString * const kClassNameKey = @"className";
NSString * const kTitleKey = @"title";

@interface TopTableBarViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) UIScrollView *titleScrollView;
@property (weak, nonatomic) UIScrollView *contentScrollView;

@property (weak, nonatomic) UIButton *selectBtn;
@property (nonatomic, strong) NSMutableArray *buttons;

@property (assign, nonatomic) BOOL isInitialize;

@end

@implementation TopTableBarViewController

- (NSMutableArray *)buttons {
    if (!_buttons) {
        _buttons = [[NSMutableArray alloc] init];
    }
    return _buttons;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blueColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if (self.viewControllerMap == nil && [self respondsToSelector:@selector(vcClass_TitleMaps)]) {
        self.viewControllerMap = [self vcClass_TitleMaps];
    }
    
    // 1. 添加标题ScrollView
    [self setupTitleScrollView];
    
    // 2. 添加内容ScrollView
    [self setupContentScrollView];
    
    // 3. 添加子视图控制器
    [self setupChildViewControllers];

    
    // 4. 添加标题按钮
    [self setupTitleButtons];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

#pragma mark - Actions
- (void)titleClick:(UIButton *)btn
{
    // 1. 颜色改变
    [self selBtn:btn];
    
    NSInteger index = [self.buttons indexOfObject:btn];
    
    // 3. 将对应的ViewContrroller's view添加到contentScrollView
    [self setupViewOfViewController:index];
    
    // 4. 移动contentScrollView
    self.contentScrollView.contentOffset = CGPointMake(kScreenW * index, 0);
}

#pragma mark - UIScrollViewDelegate
/**
 *  @brief 滑动停止时调用
 *
 *  @param scrollView scrollView
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / kScreenW;
    // 1. 将vc's view添加到contentScrollView上
    [self setupViewOfViewController:index];
    
    // 2. 选中button
    [self selBtn:[self.buttons objectAtIndex:index]];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 1. 获取左边按钮
    NSInteger leftI = scrollView.contentOffset.x / kScreenW;
    NSInteger rightI = leftI + 1;
    if (rightI >= self.buttons.count) {
        rightI = self.buttons.count - 1;
    }
    
    UIButton *leftButton = [self.buttons objectAtIndex:leftI];
    
    // 2. 获取右边按钮
    UIButton *rightButton = [self.buttons objectAtIndex:rightI];
    
    // 3. 左右边的放缩比
    CGFloat rightScale = scrollView.contentOffset.x / kScreenW - leftI;
    CGFloat leftScale = 1 - rightScale;
    
    leftButton.transform = CGAffineTransformMakeScale(0.3 * leftScale + 1, 0.3 * leftScale + 1);
    rightButton.transform = CGAffineTransformMakeScale(0.3 * rightScale + 1, 0.3 * rightScale + 1);
    
    // 4. 颜色变化
    [rightButton setTitleColor:[UIColor colorWithRed:rightScale green:0 blue:0 alpha:1] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor colorWithRed:leftScale green:0 blue:0 alpha:1] forState:UIControlStateNormal];
}
#pragma mark - Private
- (void)setupViewOfViewController:(NSInteger)index
{
    UIViewController *viewController = [self.childViewControllers objectAtIndex:index];
    CGFloat x = index * kScreenW;
    viewController.view.frame = CGRectMake(x, 0, kScreenW, CGRectGetHeight(self.contentScrollView.frame));
    [self.contentScrollView addSubview:viewController.view];
}

- (void)selBtn:(UIButton *)btn
{
    self.selectBtn.transform = CGAffineTransformIdentity;
    
    [self.selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.selectBtn = btn;
    
    // titleScrollView移动居中
    [self setupTitleButtonCenter:btn];
    
    // 缩放按钮
    btn.transform = CGAffineTransformMakeScale(1.3, 1.3);
}

- (void)setupTitleButtonCenter:(UIButton *)btn
{
    CGFloat offsetx = CGRectGetMidX(btn.frame) - 0.5 * kScreenW;
    CGFloat maxOffsetx = self.titleScrollView.contentSize.width - kScreenW;
    offsetx = MIN(MAX(0, offsetx), maxOffsetx);
    [self.titleScrollView setContentOffset:CGPointMake(offsetx, 0) animated:YES];
}

- (void)setupTitleButtons
{
    CGFloat w = 100.f;
    [self.viewControllerMap enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitle:obj[kTitleKey] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat x = idx * w;
        button.frame = CGRectMake(x, 0, w, CGRectGetHeight(self.titleScrollView.frame));
        [self.titleScrollView addSubview:button];
        [self.buttons addObject:button];
        
        if (idx == 0) {
            [self titleClick:button];
        }

    }];
    
    self.titleScrollView.contentSize = CGSizeMake(w * self.viewControllerMap.count, 0);
    self.contentScrollView.contentSize = CGSizeMake(kScreenW * self.viewControllerMap.count, 0);
}


- (void)setupChildViewControllers
{
    [self.viewControllerMap enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (self.initializeViewControllerBlock) {
            [self addChildViewController:self.initializeViewControllerBlock(NSClassFromString(obj[kClassNameKey]), obj[kTitleKey])];
        }
        else {
            [self addChildViewController:[self setupViewControllerWithClassName:NSClassFromString(obj[kClassNameKey]) title:obj[kTitleKey]]];
        }
    }];
}

- (UIViewController *)setupViewControllerWithClassName:(Class)viewControllerClass title:(NSString *)title
{
    UIViewController *viewController = [[viewControllerClass alloc] init];
    return viewController;
}

- (void)setupTitleScrollView
{
    UIScrollView *titleScrollView = [[UIScrollView alloc] init];
    titleScrollView.backgroundColor = [UIColor whiteColor];
    
    CGFloat y = self.navigationController.navigationBarHidden ? 0 : 64.f;
    titleScrollView.frame = CGRectMake(0, y, kScreenW, 44.f);
    [self.view addSubview:titleScrollView];
    
    self.titleScrollView = titleScrollView;
    
    // 属性
    titleScrollView.showsHorizontalScrollIndicator = NO;
}

- (void)setupContentScrollView
{
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    contentScrollView.backgroundColor = [UIColor greenColor];
    
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    contentScrollView.frame = CGRectMake(0, y, kScreenW, kScreenH - y);
    [self.view addSubview:contentScrollView];
    
    self.contentScrollView = contentScrollView;
    
    // 属性
    contentScrollView.showsHorizontalScrollIndicator = NO;
    contentScrollView.pagingEnabled = YES;
    contentScrollView.bounces = NO;
    contentScrollView.delegate = self;
}


- (NSArray<NSDictionary *> *)vcClass_TitleMaps
{
    return nil;
}
@end
